<template>
  <div class="container">
    <el-row>
      <!--项目数据-->
      <el-col :span="4" :xs="24">
        <el-row
          class="project"
          type="flex"
          align="middle"
          justify="space-between"
        >
          <el-col
            ><div class="project-name">
              {{ currentProject.projectName }}
            </div></el-col
          >
          <el-col :span="4">
            <el-button type="text" @click="changeProject"
              >切换</el-button
            ></el-col
          >
        </el-row>
        <el-descriptions direction="vertical" :column="1" border>
          <el-descriptions-item label="负责人">{{
            currentProject.linkMan ? currentProject.linkMan : "-"
          }}</el-descriptions-item>
          <el-descriptions-item label="负责人电话">{{
            currentProject.linkTel ? currentProject.linkTel : "-"
          }}</el-descriptions-item>
          <el-descriptions-item label="项目地址">{{
            currentProject.address ? currentProject.address : "-"
          }}</el-descriptions-item>
          <el-descriptions-item label="项目简介"
            >{{ currentProject.info ? currentProject.info : "-" }}
          </el-descriptions-item>
        </el-descriptions>
      </el-col>
      <!--设备数据-->
      <el-col :span="20" :xs="24">
        <div class="app-container">
          <el-form
            :model="queryParams"
            ref="queryForm"
            size="small"
            :inline="true"
            v-show="showSearch"
            label-width="68px"
          >
            <el-form-item label="项目节点">
              <el-cascader
                v-model="queryParams.nodeId"
                placeholder="选择项目节点"
                :options="projectNodeList"
                :props="{ value: 'id', label: 'label', checkStrictly: true }"
                filterable
                clearable
                @change="projectNodeChange"
              ></el-cascader>
            </el-form-item>

            <el-form-item>
              <el-button
                type="primary"
                icon="el-icon-search"
                size="mini"
                @click="handleQuery"
                >搜索</el-button
              >
              <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
                >重置</el-button
              >
            </el-form-item>
          </el-form>
          <el-row :gutter="10" class="mb8">
            <right-toolbar
              :showSearch.sync="showSearch"
              @queryTable="getList"
            ></right-toolbar>
          </el-row>
          <el-row>
            <div class="title">碳足迹</div>
            <div id="main"></div>
          </el-row>
        </div>
      </el-col>
    </el-row>
    <el-drawer
      :title="drawerTitle"
      :visible.sync="drawer"
      :before-close="drawerHandleClose"
      append-to-body
      destroy-on-close
      :wrapperClosable="false"
      size="60%"
    >
      <div class="app-container">
        <el-card class="box-card">
          <div slot="header" class="clearfix">
            <span>实时数据</span>
            <span
              style="float: right; color: #cccccc; font-size: 14px"
              type="text"
              ><i class="el-icon-time"></i>&nbsp;&nbsp;&nbsp;{{
                detailParam.last.createDate ? detailParam.last.createDate : "-"
              }}</span
            >
          </div>
          <el-descriptions direction="vertical" :column="3" border>
            <el-descriptions-item label="电压/V">{{
              detailParam.last.voltage ? detailParam.last.voltage : "-"
            }}</el-descriptions-item>
            <el-descriptions-item label="电流/A">{{
              detailParam.last.electricCurrent
                ? detailParam.last.electricCurrent
                : "-"
            }}</el-descriptions-item>
            <el-descriptions-item label="总有功功率/KW">{{
              detailParam.last.totalActivePower
                ? detailParam.last.totalActivePower
                : "-"
            }}</el-descriptions-item>
          </el-descriptions>
        </el-card>
        <el-card class="box-card">
          <div slot="header" class="clearfix">
            <span>历史曲线</span>
          </div>
          <div>
            <el-select
              v-model="detailParam.type"
              placeholder="请选择"
              style="margin-right: 10px"
              @change="getDetailStatistic"
            >
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
            <el-date-picker
              @change="getDetailStatistic"
              v-model="detailParam.date"
              type="date"
              placeholder="选择日期"
              value-format="yyyy-MM-dd"
            >
            </el-date-picker>
          </div>
        </el-card>
      </div>
    </el-drawer>
    <el-dialog
      title="选择项目"
      :visible.sync="open"
      width="600px"
      append-to-body
      :close-on-click-modal="false"
    >
      <div>
        <el-radio-group v-model="tempProjectId">
          <el-radio
            :label="item.id"
            v-for="(item, index) in projectList"
            :key="index"
            >{{ item.projectName }}</el-radio
          >
        </el-radio-group>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submit">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import * as echarts from "echarts/core";
import { SankeyChart } from "echarts/charts";
import { CanvasRenderer } from "echarts/renderers";
echarts.use([SankeyChart, CanvasRenderer]);
//以上为引入的echarts
import {
  listDevice,
  listProject,
  listProjectNode,
  getProjectSummary,
  getDeviceLastData,
  getDeviceStatistic,
} from "@/api/bussiness/footprint";
export default {
  name: "Footprint",
  dicts: [
    "buss_device_protocol",
    "buss_device_node_type",
    "buss_device_status",
  ],
  data() {
    return {
      //筛选选项
      options: [
        {
          value: "ua",
          label: "电压",
        },
        {
          value: "ia",
          label: "电流",
        },
        {
          value: "p",
          label: "总有功功率",
        },
      ],
      //以上数据均为测试数据
      //切换项目选择项目ID
      tempProjectId: undefined,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 设备管理表格数据
      deviceList: [],
      //当前项目信息
      currentProject: undefined,
      currentProjectSummary: undefined,
      currentDetail: {},
      //项目节点数据
      projectNodeList: [],
      //详情
      detailParam: {
        last: {},
        statistic: {},
        type: "ua",
        date:
          new Date().getFullYear() +
          "-" +
          (new Date().getMonth() + 1 < 10
            ? "0" + (new Date().getMonth() + 1)
            : new Date().getMonth() + 1) +
          "-" +
          (new Date().getDate() < 10
            ? "0" + new Date().getDate()
            : new Date().getDate()),
      },
      //项目数据
      projectList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      drawer: false,
      drawerTitle: "",
      //切换项目弹框
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        projectId: null,
        deviceName: null,
        status: null,
        nodeId: null,
      },
    };
  },
  created() {
    this.getProject();
  },
  mounted() {
    setTimeout(() => {
      this.initMain();
    }, 500);
  },
  methods: {
    getProject() {
      listProject().then((res) => {
        this.projectList = res.data;
        if (this.projectList && this.projectList.length > 0) {
          this.currentProject = this.projectList[0];
          //请求当前节点汇总信息 电表总数 在线数 今日用电数
          //请求当前项目下的电表数据
          this.loadData();
        } else {
          this.$modal.alertWarning("暂无项目信息");
        }
      });
    },
    getProjectNodeList() {
      listProjectNode({ projectId: this.currentProject.id }).then((res) => {
        this.projectNodeList = res.data;
      });
    },
    projectNodeChange(value) {
      this.queryParams.nodeId = value[value.length - 1];
      this.handleQuery();
    },
    submit() {
      if (this.tempProjectId != this.currentProject.id) {
        //切换成功，重新加载所有数据
        for (let item of this.projectList) {
          if (item.id == this.tempProjectId) {
            this.currentProject = item;
          }
        }
        //查询所有新数据
        this.loadData();
        this.open = false;
      }
    },
    getProjectSummary() {
      getProjectSummary({ projectId: this.currentProject.id }).then((res) => {
        this.currentProjectSummary = res.data;
      });
    },
    loadData() {
      this.queryParams.pageNum = 1;
      this.queryParams.deviceName = null;
      this.queryParams.deviceName = null;
      this.queryParams.status = null;
      this.nodeId = null;
      this.queryParams.projectId = this.currentProject.id;
      this.tempProjectId = this.currentProject.id;
      this.getList();
      this.getProjectNodeList();
      this.getProjectSummary();
    },
    // 取消按钮
    cancel() {
      this.open = false;
    },
    detail(item) {
      this.drawerTitle = item.deviceName;
      this.drawer = true;
      this.currentDetail = item;
      //获取详情数据
      getDeviceLastData(item.id).then((res) => {
        this.detailParam.last = res.data;
      });
      //获取最近曲线数据
      this.getDetailStatistic();
    },
    getDetailStatistic() {
      getDeviceStatistic({
        id: this.currentDetail.id,
        field: this.detailParam.type,
        date: this.detailParam.date,
      }).then((res) => {
        this.detailParam.statistic = res.data;
        this.initEcharts();
      });
    },
    drawerHandleClose(done) {
      done();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    //切换项目
    changeProject() {
      this.open = true;
    },
    /** 查询设备管理列表 */
    getList() {
      this.loading = true;
      listDevice(this.queryParams).then((response) => {
        this.deviceList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    initMain() {
      let chartDom = document.getElementById("main");
      let myChart = echarts.init(chartDom);
      let option;

      option = {
        color: [
          "#5470c6",
          "#fac858",
          "#ee6666",
          "#73c0de",
          "#3ba272",
          "#fc8452",
          "#ea7ccc",
        ],
        series: {
          type: "sankey",
          layout: "none",
          emphasis: {
            focus: "adjacency",
          },
          lineStyle: {
            color: "gradient",
            curveness: 0.5,
          },
          data: [
            {
              name: "业达科技园区：电5000kw/h，水：400t",
            },
            {
              name: "A栋：电1000kw/h，水：100t",
            },
            {
              name: "B栋：电2100kw/h，水：150t",
            },
            {
              name: "C栋：电1900kw/h，水：150t",
            },
            {
              name: "A1层：电250kw/h，水：25t",
            },
            {
              name: "A2层：电250kw/h，水：25t",
            },
            {
              name: "A3层：电250kw/h，水：25t",
            },
            {
              name: "A4层：电250kw/h，水：25t",
            },
            {
              name: "B1层：电425kw/h，水：30t",
            },
            {
              name: "B2层：电425kw/h，水：30t",
            },
            {
              name: "B3层：电425kw/h，水：30t",
            },
            {
              name: "B4层：电425kw/h，水：30t",
            },
            {
              name: "B5层：电425kw/h，水：30t",
            },
            {
              name: "C1层：电380kw/h，水：30t",
            },
            {
              name: "C2层：电380kw/h，水：30t",
            },
            {
              name: "C3层：电380kw/h，水：30t",
            },
            {
              name: "C4层：电380kw/h，水：30t",
            },
            {
              name: "C5层：电380kw/h，水：30t",
            },
            {
              name: "停车场：电380kw/h，水：30t",
            },
          ],
          links: [
            {
              source: "业达科技园区：电5000kw/h，水：400t",
              target: "A栋：电1000kw/h，水：100t",
              value: 32,
            },
            {
              source: "业达科技园区：电5000kw/h，水：400t",
              target: "B栋：电2100kw/h，水：150t",
              value: 40,
            },
            {
              source: "业达科技园区：电5000kw/h，水：400t",
              target: "C栋：电1900kw/h，水：150t",
              value: 40,
            },
            {
              source: "A栋：电1000kw/h，水：100t",
              target: "A1层：电250kw/h，水：25t",
              value: 8,
            },
            {
              source: "A栋：电1000kw/h，水：100t",
              target: "A2层：电250kw/h，水：25t",
              value: 8,
            },
            {
              source: "A栋：电1000kw/h，水：100t",
              target: "A3层：电250kw/h，水：25t",
              value: 8,
            },
            {
              source: "A栋：电1000kw/h，水：100t",
              target: "A4层：电250kw/h，水：25t",
              value: 8,
            },
            {
              source: "B栋：电2100kw/h，水：150t",
              target: "B1层：电425kw/h，水：30t",
              value: 8,
            },
            {
              source: "B栋：电2100kw/h，水：150t",
              target: "B2层：电425kw/h，水：30t",
              value: 8,
            },
            {
              source: "B栋：电2100kw/h，水：150t",
              target: "B3层：电425kw/h，水：30t",
              value: 8,
            },
            {
              source: "B栋：电2100kw/h，水：150t",
              target: "B4层：电425kw/h，水：30t",
              value: 8,
            },
            {
              source: "B栋：电2100kw/h，水：150t",
              target: "B5层：电425kw/h，水：30t",
              value: 8,
            },
            {
              source: "C栋：电1900kw/h，水：150t",
              target: "C1层：电380kw/h，水：30t",
              value: 8,
            },
            {
              source: "C栋：电1900kw/h，水：150t",
              target: "C2层：电380kw/h，水：30t",
              value: 8,
            },
            {
              source: "C栋：电1900kw/h，水：150t",
              target: "C3层：电380kw/h，水：30t",
              value: 8,
            },
            {
              source: "C栋：电1900kw/h，水：150t",
              target: "C4层：电380kw/h，水：30t",
              value: 8,
            },
            {
              source: "C栋：电1900kw/h，水：150t",
              target: "C5层：电380kw/h，水：30t",
              value: 8,
            },
            {
              source: "业达科技园区：电5000kw/h，水：400t",
              target: "停车场：电380kw/h，水：30t",
              value: 22,
            },
          ],
        },
      };

      option && myChart.setOption(option);
    },
  },
};
</script>

<style>
.full-height {
  height: 100%;
}
.project {
  background-color: #f2f2f2f2;
  padding: 10px 10px;
}
.container {
  padding: 0px;
  font-size: 14px;
}
.text-center {
  text-align: center;
  padding: 10px;
}
.label {
  color: #383838;
}
.value {
}
.box-card {
  margin-bottom: 10px;
}
.info {
  margin-top: 10px;
  color: #383838;
}
#main {
  height: 300px;
  width: 100%;
}
.title {
  border-left: 3px solid rgb(38, 58, 158);
  padding-left: 5px;
  margin-bottom: 20px;
  text-align: left;
}
#month,
#year {
  height: 300px;
}
.txt-center {
  text-align: center !important;
}
</style>
